package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Customer;
import bean.Room;
import common.base.BaseServlet;
import service.RoomService;
import service.UserService;
import service.impl.RoomServiceImpl;
import service.impl.UserServiceImpl;


@WebServlet("/HomeServlet")
public class HomeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public String ui(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//取Cookie
		String usid = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("usid") && cookie.getMaxAge() > 0) {
					usid = cookie.getValue();
				}
			}
		}
		
		//登录过
		if (usid != null) {
			UserService userService = new UserServiceImpl();
			Customer user = userService.queryById(usid);
			//写入session
			request.getSession().setAttribute("user", user);
		}
		
		//取热门数据，放入request域中
		RoomService roomsService = new RoomServiceImpl();
		List<Room> hotRooms = roomsService.queryHotRoom();
		request.setAttribute("hotRooms", hotRooms);
		
		
		return "/user/index.jsp";	
	}

	public String adminUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/admin/index.jsp";
	}
	
}
